﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption

Partial Class UploadOldListing
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadUpload()
    End Sub

    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        Dim ListType, Road, PNO, FLR, LOC, PROJ, OWNER, TYPE, OWNERCONTACT, OTHERCONTACT, ADDRESS, STATUSDELETE As String

        If ListTypeDropDown.Text <> String.Empty And ListTypeDropDown.Text <> "ALL" Then
            ListType = " AND LISTTYPE = '" & ListTypeDropDown.Text & "'"
        Else
            ListType = String.Empty
        End If

        If txtRoad.Text <> String.Empty Then
            Road = " AND Road LIKE @Road"
            objDB.AddParameter("Road", Data.SqlDbType.NVarChar, "%" & txtRoad.Text.Trim & "%")
        Else
            Road = String.Empty
        End If

        If txtPNO.Text <> String.Empty Then
            PNO = " AND PNO LIKE @PNO"
            objDB.AddParameter("PNO", Data.SqlDbType.NVarChar, "%" & txtPNO.Text.Trim & "%")
        Else
            PNO = String.Empty
        End If

        If txtFLR.Text <> String.Empty Then
            FLR = " AND FLR LIKE @FLR"
            objDB.AddParameter("FLR", Data.SqlDbType.NVarChar, "%" & txtFLR.Text.Trim & "%")
        Else
            FLR = String.Empty
        End If

        If txtLOC.Text <> String.Empty Then
            Loc = " AND LOC LIKE @LOC"
            objDB.AddParameter("LOC", Data.SqlDbType.NVarChar, "%" & txtLOC.Text.Trim & "%")
        Else
            Loc = String.Empty
        End If

        If txtPROJ.Text <> String.Empty Then
            PROJ = " AND PROJ LIKE @PROJ"
            objDB.AddParameter("PROJ", Data.SqlDbType.NVarChar, "%" & txtPROJ.Text.Trim & "%")
        Else
            PROJ = String.Empty
        End If

        If txtOWNER.Text <> String.Empty Then
            OWNER = " AND OWNER LIKE @OWNER"
            objDB.AddParameter("OWNER", Data.SqlDbType.NVarChar, "%" & txtOWNER.Text.Trim & "%")
        Else
            OWNER = String.Empty
        End If

        If txtTYPE.Text <> String.Empty Then
            TYPE = " AND TYPE LIKE @TYPE"
            objDB.AddParameter("TYPE", Data.SqlDbType.NVarChar, "%" & txtTYPE.Text.Trim & "%")
        Else
            TYPE = String.Empty
        End If

        If txtOWNERCONTACT.Text <> String.Empty Then
            OWNERCONTACT = " AND (TELHOME LIKE @OWNERCONTACT OR TELOFF LIKE @OWNERCONTACT OR TELHP LIKE @OWNERCONTACT)"
            objDB.AddParameter("OWNERCONTACT", Data.SqlDbType.NVarChar, "%" & txtOWNERCONTACT.Text.Trim & "%")
        Else
            OWNERCONTACT = String.Empty
        End If

        If txtOTHERCONTACT.Text <> String.Empty Then
            OTHERCONTACT = " AND (CONTACT LIKE @OTHERCONTACT OR CONAME LIKE @OTHERCONTACT)"
            objDB.AddParameter("OTHERCONTACT", Data.SqlDbType.NVarChar, "%" & txtOTHERCONTACT.Text.Trim & "%")
        Else
            OTHERCONTACT = String.Empty
        End If

        If txtADDRESS.Text <> String.Empty Then
            ADDRESS = " AND (ADD1=@ADDRESS OR ADD2=@ADDRESS OR ADD3=@ADDRESS)"
            objDB.AddParameter("ADDRESS", Data.SqlDbType.NVarChar, "%" & txtADDRESS.Text.Trim & "%")
        Else
            ADDRESS = String.Empty
        End If

        If StatusDeleteDropDown.Text = "NO" Then
            STATUSDELETE = " STATUSDELETE IS NULL "
            objDB.dbVariable.SQLString = "SELECT ID, LISTTYPE, ROAD, PNO, FLR, LOC, PROJ, OWNER, " & _
                                        "TYPE, ADDS, TELHOME, TELOFF, TELHP, REMARK,REMARK1,REMARK2, CONTACT, CONAME,ADD1,ADD2,ADD3,ADD4,DeletorId, DeletedDate,StatusDelete,'' as UserName FROM " & _
                                        "AW_OldListing WHERE " & STATUSDELETE & ListType & Road & PNO & FLR & LOC & _
                                        PROJ & OWNER & TYPE & OWNERCONTACT & OTHERCONTACT & ADDRESS & lblhidSortType.Text
        Else
            STATUSDELETE = " a.STATUSDELETE=1 "
            objDB.dbVariable.SQLString = "SELECT ID, LISTTYPE, ROAD, PNO, FLR, LOC, PROJ, OWNER, " & _
                                        "TYPE, ADDS, TELHOME, TELOFF, TELHP, REMARK,REMARK1,REMARK2, CONTACT, CONAME,ADD1,ADD2,ADD3,ADD4,a.DeletorId, a.DeletedDate,a.StatusDelete, b.UserName FROM " & _
                                        "AW_OldListing a " & _
                                        "LEFT JOIN AW_Staff b ON b.StaffId = a.DeletorId " & _
                                        "WHERE " & STATUSDELETE & ListType & Road & PNO & FLR & LOC & _
                                        PROJ & OWNER & TYPE & OWNERCONTACT & OTHERCONTACT & ADDRESS & lblhidSortType.Text
        End If


        objDB.FillData(TempDT, "AW_Auction", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repUpload.DataSource = objPaged
        repUpload.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkCreateDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateDate.Click
        If lnkCreateDate.Text = "CONAME▼" Then
            lnkCreateDate.Text = "CONAME▲"
        Else
            lnkCreateDate.Text = "CONAME▼"
        End If

        lblhidSortType.Text = " ORDER BY CONAME " & SortType(lnkCreateDate.Text)
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkDaerah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDaerah.Click
        If lnkDaerah.Text = "LISTTYPE▼" Then
            lnkDaerah.Text = "LISTTYPE▲"
        Else
            lnkDaerah.Text = "LISTTYPE▼"
        End If

        lblhidSortType.Text = " ORDER BY LISTTYPE " & SortType(lnkDaerah.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkMukim_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMukim.Click
        If lnkMukim.Text = "ROAD▼" Then
            lnkMukim.Text = "ROAD▲"
        Else
            lnkMukim.Text = "ROAD▼"
        End If

        lblhidSortType.Text = " ORDER BY ROAD " & SortType(lnkMukim.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkSek_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSek.Click
        If lnkSek.Text = "PNO▼" Then
            lnkSek.Text = "PNO▲"
        Else
            lnkSek.Text = "PNO▼"
        End If

        lblhidSortType.Text = " ORDER BY PNO " & SortType(lnkSek.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkJenisLot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkJenisLot.Click
        If lnkJenisLot.Text = "FLR▼" Then
            lnkJenisLot.Text = "FLR▲"
        Else
            lnkJenisLot.Text = "FLR▼"
        End If

        lblhidSortType.Text = " ORDER BY FLR " & SortType(lnkJenisLot.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkNoLot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNoLot.Click
        If lnkNoLot.Text = "LOC▼" Then
            lnkNoLot.Text = "LOC▲"
        Else
            lnkNoLot.Text = "LOC▼"
        End If

        lblhidSortType.Text = " ORDER BY LOC " & SortType(lnkNoLot.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkTkhPenilaian_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTkhPenilaian.Click
        If lnkTkhPenilaian.Text = "OWNER▼" Then
            lnkTkhPenilaian.Text = "OWNER▲"
        Else
            lnkTkhPenilaian.Text = "OWNER▼"
        End If

        lblhidSortType.Text = " ORDER BY OWNER " & SortType(lnkTkhPenilaian.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkLuasLot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLuasLot.Click
        If lnkLuasLot.Text = "TYPE▼" Then
            lnkLuasLot.Text = "TYPE▲"
        Else
            lnkLuasLot.Text = "TYPE▼"
        End If

        lblhidSortType.Text = " ORDER BY TYPE " & SortType(lnkLuasLot.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkJenisHartanah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkJenisHartanah.Click
        If lnkJenisHartanah.Text = "TELHOME/OFF/HP▼" Then
            lnkJenisHartanah.Text = "TELHOME/OFF/HP▲"
        Else
            lnkJenisHartanah.Text = "TELHOME/OFF/HP▼"
        End If

        lblhidSortType.Text = " ORDER BY TELHOME " & SortType(lnkJenisHartanah.Text) & _
                            ", TELOFF " & SortType(lnkJenisHartanah.Text) & ", TELOFF " & SortType(lnkJenisHartanah.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkAlamat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAlamat.Click
        If lnkAlamat.Text = "ID▼" Then
            lnkAlamat.Text = "ID▲"
        Else
            lnkAlamat.Text = "ID▼"
        End If

        lblhidSortType.Text = " ORDER BY ID " & SortType(lnkAlamat.Text)
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkKawasan_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkKawasan.Click

    End Sub

    Protected Sub lnkPemberi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPemberi.Click
        If lnkPemberi.Text = "REMARKS▼" Then
            lnkPemberi.Text = "REMARKS▲"
        Else
            lnkPemberi.Text = "REMARKS▼"
        End If

        lblhidSortType.Text = " ORDER BY REMARK " & SortType(lnkPemberi.Text) & _
                            ", REMARK1 " & SortType(lnkPemberi.Text) & ", REMARK2 " & SortType(lnkPemberi.Text)
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPenerima_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPenerima.Click
        If lnkPenerima.Text = "DELETE BY▼" Then
            lnkPenerima.Text = "DELETE BY▲"
        Else
            lnkPenerima.Text = "DELETE BY▼"
        End If

        lblhidSortType.Text = " ORDER BY Username " & SortType(lnkPenerima.Text) & _
                            ", id " & SortType(lnkCreateDate.Text) & ", LISTTYPE " & SortType(lnkDaerah.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkBalasan_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBalasan.Click
        If lnkBalasan.Text = "CONTACT▼" Then
            lnkBalasan.Text = "CONTACT▲"
        Else
            lnkBalasan.Text = "CONTACT▼"
        End If

        lblhidSortType.Text = " ORDER BY CONTACT " & SortType(lnkBalasan.Text) 
        btnSearch_Click(sender, e)
    End Sub

    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
        End If
        If e.CommandName = "DeleteList" Then
            Dim recordid As String
            recordid = e.CommandArgument.ToString
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim TempDT As New Data.DataTable

            objDB.OpenConnection()
            objDB.InitDbTrans()
            objDB.dbVariable.SQLString = "UPDATE AW_OldListing SET StatusDelete=@StatusDelete,DeletorId=@DeletorId,DeletedDate=@DeletedDate WHERE ID=@id"
            objDB.AddParameter("@id", Data.SqlDbType.BigInt, recordid)
            objDB.AddParameter("@StatusDelete", Data.SqlDbType.Int, 1)
            objDB.AddParameter("@DeletorId", Data.SqlDbType.UniqueIdentifier, New Guid(Session.Item("StaffId").ToString()))
            objDB.AddParameter("@DeletedDate", Data.SqlDbType.DateTime, Now)
            objDB.ExecuteQuery()
            objDB.ClearParameter()
            objDB.DbTransCommit()
            objDB.CloseConnection()
            btnSearch_Click(source, e)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '1-Back Page
            '2-Page Size
            '3-Current Page
            '4-Daerah
            '5-Mukim
            '6-Sek
            '7-Jenis Lot
            '8-No Lot
            '9-Trkh Penilaian
            '10-Luas Lot
            '11-Jenis Hartanah
            '12-Alamat
            '13-Kawasan
            '14-Pemberi
            '15-Penerima
            '16-Balasan From
            '17-Balasan To
            If arr(0).ToString.Contains("UploadOldListing.aspx") Then
                PageSize = CInt(arr(1).ToString)
                CurrentPage = CInt(arr(2).ToString)
                ListTypeDropDown.SelectedValue = arr(3).ToString
                txtRoad.Text = arr(4).ToString
                txtPNO.Text = arr(5).ToString
                txtFLR.Text = arr(6).ToString
                txtLOC.Text = arr(7).ToString
                txtPROJ.Text = arr(8).ToString
                txtOWNER.Text = arr(9).ToString
                txtTYPE.Text = arr(10).ToString
                txtOWNERCONTACT.Text = arr(11).ToString
                txtOTHERCONTACT.Text = arr(12).ToString
                txtADDRESS.Text = arr(13).ToString
                Session.Remove("Back")
                LoadUpload()
            Else
                Session.Remove("Back")
            End If

        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub

    Protected Sub lnkPROJ_Click(sender As Object, e As System.EventArgs) Handles lnkPROJ.Click
        If lnkPROJ.Text = "PROJ▼" Then
            lnkPROJ.Text = "PROJ▲"
        Else
            lnkPROJ.Text = "PROJ▼"
        End If

        lblhidSortType.Text = " ORDER BY PROJ " & SortType(lnkDaerah.Text)
    End Sub

    Protected Sub lnkADDS_Click(sender As Object, e As System.EventArgs) Handles lnkADDS.Click
        If lnkADDS.Text = "ADDS▼" Then
            lnkADDS.Text = "ADDS▲"
        Else
            lnkADDS.Text = "ADDS▼"
        End If

        lblhidSortType.Text = " ORDER BY ADDS " & SortType(lnkDaerah.Text)
    End Sub

    Protected Sub lnkADDS2_Click(sender As Object, e As System.EventArgs) Handles lnkADDS2.Click
        If lnkADDS.Text = "ADDS1,2,3,4▼" Then
            lnkADDS.Text = "ADDS1,2,3,4▲"
        Else
            lnkADDS.Text = "ADDS1,2,3,4▼"
        End If

        lblhidSortType.Text = " ORDER BY ADD1 " & SortType(lnkDaerah.Text) & _
            ", ADD2 " & SortType(lnkDaerah.Text) & ", ADD3 " & SortType(lnkDaerah.Text)
    End Sub

    Protected Sub lnkDeletedDate_Click(sender As Object, e As System.EventArgs) Handles lnkDeletedDate.Click
        If lnkDeletedDate.Text = "DELETE DATE▼" Then
            lnkDeletedDate.Text = "DELETE DATE▲"
        Else
            lnkDeletedDate.Text = "DELETE DATE▼"
        End If

        lblhidSortType.Text = " ORDER BY DeletedDate " & SortType(lnkDaerah.Text) & _
            ", ID " & SortType(lnkDaerah.Text)
    End Sub
End Class
